//
//  AttackViewController.m
//  GiftNote
//
//  Created by wsh on 15/8/12.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "AttackViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "INHeader.h"
#import "AttackModel.h"
#import "AttackHeaderModel.h"
#import "channelsModel.h"
#import "Screen.h"
#import "ListCollectionViewCell.h"
#import "PKRequestManager.h"
#import "AttackTitleCollectionReusableView.h"
#import "AttackTitleFooterCollectionReusableView.h"
#import "AttackTitleCollectionReusableView2.h"
#import "HeaderCollectionViewCell.h"
#import "CommonTableViewController.h"
#import "AttackAllTopicTableViewController.h"


@interface AttackViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *dataArry;   // 列表的数组
@property (nonatomic, strong) NSMutableArray *headerArry; // 专题的数组
@property (nonatomic, strong) UICollectionViewController *mainCollectionVC;

@property (nonatomic, strong) CommonTableViewController *commonTableVC;

@end

@implementation AttackViewController

#pragma mark --- 懒加载
- (NSMutableArray *)dataArry{
    if (_dataArry == nil) {
        self.dataArry = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArry;
}

- (NSMutableArray *)headerArry{
    
    if (_headerArry == nil) {
        self.headerArry = [NSMutableArray arrayWithCapacity:0];
    }
    return _headerArry;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self requestData];
    [self requestDataOfHeader];
    
    [self createCollectionVC];
    
    
    [self.mainCollectionVC.collectionView registerClass:[HeaderCollectionViewCell class] forCellWithReuseIdentifier:@"headerCell"];
    [self.mainCollectionVC.collectionView registerClass:[ListCollectionViewCell class] forCellWithReuseIdentifier:@"listCell"];
    
    [self.mainCollectionVC.collectionView registerClass:[AttackTitleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"titleCell"];
    [self.mainCollectionVC.collectionView registerClass:[AttackTitleCollectionReusableView2 class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"titleCell2"];
    [self.mainCollectionVC.collectionView registerClass:[AttackTitleFooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerCell"];
    [self.mainCollectionVC.collectionView registerClass:[AttackTitleFooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerCell2"];
    
}



- (void)createCollectionVC{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 30);
    layout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 20);
    layout.minimumInteritemSpacing = 30;
    
    _mainCollectionVC = [[UICollectionViewController alloc] initWithCollectionViewLayout:layout];
    _mainCollectionVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH , SCREEN_HEIGHT );
    
    _mainCollectionVC.collectionView.backgroundColor = [UIColor whiteColor];
    
    _mainCollectionVC.collectionView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:_mainCollectionVC.view];
}



#pragma mark --- UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 1;
    } else {
        
        AttackModel *model = _dataArry[section - 1];
        return model.channels.count;
    }
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.dataArry.count + 1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        HeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"headerCell" forIndexPath:indexPath];
        cell.dataArray = _headerArry;
        cell.nac = self.nac;
        return cell;
        
    } else {
        ListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"listCell" forIndexPath:indexPath];
        AttackModel *attModel = _dataArry[indexPath.section -1];
        channelsModel *channelModel = attModel.channels[indexPath.item];
        
        cell.model = channelModel;
        return cell;
    }
}




- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    NSMutableString *str = nil;
    
    if (indexPath.section == 0) {
        str = [@"专题" mutableCopy];
    } else {
        str = [((AttackModel *)(_dataArry[indexPath.section - 1])).name mutableCopy];
    }
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        if (indexPath.section == 0) {
            AttackTitleCollectionReusableView2 *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"titleCell2" forIndexPath:indexPath];
            view.backgroundColor = [UIColor whiteColor];
            
            [view.button addTarget:self action:@selector(EnterNext) forControlEvents:UIControlEventTouchUpInside]; ;
            view.titile = str;
            return view;
        } else {
            AttackTitleCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"titleCell" forIndexPath:indexPath];
            view.backgroundColor = [UIColor whiteColor];
            view.titile = str;
            return view;
        }
        
    } else {
        
        if (indexPath.section != _dataArry.count ) {
            AttackTitleFooterCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerCell" forIndexPath:indexPath];
            return view;
        } else {
            AttackTitleFooterCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerCell2" forIndexPath:indexPath];
            view.mainImageView.backgroundColor = [UIColor whiteColor];
            return view;
        }
    }
}



#pragma mark --- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    AttackModel *model = _dataArry[indexPath.section - 1];
    channelsModel *channelsModel = model.channels[indexPath.item];
    NSInteger num = [channelsModel.channelsid integerValue];
    NSString *str = channelsModel.name;
    
    NSString *urlString = [NSString stringWithFormat:kGuideDetailSortURL,(long)num];
    
    //  上父类推出下一个界面
    self.myBlock(urlString,str);
}



#pragma mark --- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH - 20  , 120);
    } else {
        return CGSizeMake((SCREEN_WIDTH - 120) / 5.0, (SCREEN_WIDTH - 120) / 5.0 + 30);
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 10, 0, 10);
}




#pragma mark --- 列表的解析数据
-(void)requestData{
    [PKRequestManager manager:GET requestWith:KGuideSortURL parDic:nil finish:^(NSData *data) {
        
        if (data == nil) {
            return ;
        }
        NSDictionary *parDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        NSArray *arr = parDic[@"data"][@"channel_groups"];
        for (NSDictionary *dic in arr) {
            AttackModel *model = [[AttackModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArry addObject:model];
        }
        _mainCollectionVC.collectionView.delegate = self;
        _mainCollectionVC.collectionView.dataSource = self;
        
        [self.mainCollectionVC.collectionView reloadData];
    } error:^(NSError *error) {
        
    }];
}



#pragma mark --- 专题的解析
-(void)requestDataOfHeader{
    
    [PKRequestManager manager:GET requestWith:KBannerURL parDic:nil finish:^(NSData *data) {
        if (data == nil) {
            return ;
        }
        
        NSDictionary *parDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        NSArray *arr = parDic[@"data"][@"collections"];
        for (NSDictionary *dic in arr) {
            AttackHeaderModel *model = [[AttackHeaderModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.headerArry addObject:model];
        }
        
        [self.mainCollectionVC.collectionView reloadData];
        
    } error:^(NSError *error) {
        
    }];
    
}



#pragma mark --- action
- (void)EnterNext{
    AttackAllTopicTableViewController *allVC = [[AttackAllTopicTableViewController alloc] initWithStyle:UITableViewStylePlain];
    allVC.urlString = kAllBannerURL;
    allVC.nac = self.nac;
    [self.nac pushViewController:allVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
